package com.hxx.day9;

/**
 * 同步锁
 */
public class TestDemo5 {
        /**
         * 测试同步锁
         */
        public static void main(String[] args) {
            Runnable r1 = new Processor1();
            Thread t1 = new Thread(r1, "t1");
            Thread t2 = new Thread(r1, "t2");
            t1.start();
            t2.start();
        }
}
class Processor1 implements Runnable {
    //定义成员变量s，作为累加变量
    int s = 0;
    //public synchronized void run() {
    public  void run() {
        //10000 code
        //定义同步块，锁对象为this
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                s += i;
            }
            System.out.println(Thread.currentThread().getName() + ", s=" + s);
            s = 0;
        }
    }

   /* public void run() {
        //定义局部变量s，作为累加变量
        int s = 0;
        for (int i=0; i<10; i++) {
            s+=i;
        }
        System.out.println(Thread.currentThread().getName() + ", s=" + s);
    }*/
}
